﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;

namespace WebC
{
    public partial class base_companybrand : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public void loadInfo()
        {
            BaseInfo baseinfo = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                baseinfo = new BaseInfo();
                if (!checkModelRightById(526, ref sModelName, (clsBaseLib)baseinfo))
                {
                    errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                    return;
                }
                sModelFullName = sModelName;
                lbCompanyName.Text = baseinfo.getCompanyNameById(m_Id);
                ds = baseinfo.getCompanyBrand(m_Id);
            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (baseinfo != null)
                {
                    baseinfo.Dispose();
                }
            }
            if (ds != null)
            {
                Styker.WebControls.TreeView.TreeNode tnnew = new Styker.WebControls.TreeView.TreeNode();
                tnnew.Value ="0";
                tnnew.Text = "国产";                
                tnnew.Checkable = false;
                tnnew.Checked = false;
                DataTable dt = ds.Tables[0];
                int iRowCount = dt.Rows.Count;
                for (int i = 0; i < iRowCount; i++)
                {
                    Styker.WebControls.TreeView.TreeNode tnChild = new Styker.WebControls.TreeView.TreeNode();
                    tnChild.Value = Convert.ToString(dt.Rows[i]["id"]);
                    tnChild.Text = Convert.ToString(dt.Rows[i]["name"]);
                    tnChild.Checked = Convert.ToInt32(dt.Rows[i]["ischeck"]) == 1;
                    tnChild.Checkable = true;
                    tnnew.AddNode(tnChild);
                }
                tvCompanyBrand.AddNode(tnnew);
                tnnew = new Styker.WebControls.TreeView.TreeNode();
                tnnew.Value = Convert.ToString(1);
                tnnew.Text = Convert.ToString("进口");
                tnnew.Checkable = false;
                tnnew.Checked = false;
                dt = ds.Tables[1];
                iRowCount = dt.Rows.Count;
                for (int i = 0; i < iRowCount; i++)
                {
                    Styker.WebControls.TreeView.TreeNode tnChild = new Styker.WebControls.TreeView.TreeNode();
                    tnChild.Value = Convert.ToString(dt.Rows[i]["id"]);
                    tnChild.Text = Convert.ToString(dt.Rows[i]["name"]);
                    tnChild.Checked = Convert.ToInt32(dt.Rows[i]["ischeck"]) == 1;
                    tnChild.Checkable = true;
                    tnnew.AddNode(tnChild);
                }
                tvCompanyBrand.AddNode(tnnew);
            }
        }

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sIds = "0" + tree.getTreeNodes(tvCompanyBrand.Nodes);
            BaseInfo baseinfo = null;
            try
            {
                baseinfo = new BaseInfo();
                baseinfo.setCompanyBrand(m_Id, sIds);
            }
            catch (Exception ex)
            {
                errorUrl(Convert.ToString(ex));
            }
            finally
            {
                if (baseinfo != null)
                {
                    baseinfo.Dispose();
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "savsuc", "<script>alert('保存成功！');</script>");
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("companylist.aspx");
        }
    }

}